<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:checkLogin.php');
}

$m=0;
$createFrom = isset($_POST['createFrom']) ? $_POST['createFrom'] : 0;

$loanArray = array();
$h=0;
$selectLoan = "SELECT loanId, addOnLoanId , loan.partyId ,loan.itemId , loan.value, loanAmount, item.itemName, loan.loanVar, loan.loanVarNo, 
                      loan.weight, loan.itemImage, itemlocation.itemlocationName, loanDate, monthlyInterest, monthlyInterestRs
                 FROM loan
            LEFT JOIN item ON loan.itemId = item.itemId
            LEFT JOIN itemlocation ON loan.itemlocationId = itemlocation.itemlocationId
                WHERE loanId=".$_GET['createFrom'];
$selectLoanRes = mysql_query($selectLoan);
while($loanRow = mysql_fetch_array($selectLoanRes))
{
	$selectLoanAmount = "SELECT SUM(loanAmount) AS totaLoanAmount
	                       FROM loan
	                      WHERE itemId = ".$loanRow['itemId'];
  $selectLoanAmountRes = mysql_query($selectLoanAmount);
  $selectLoanAmountRow = mysql_fetch_array($selectLoanAmountRes); 
	$loanArray['loanId']            = $loanRow['loanId'];
	$loanArray['addOnLoanId']       = $loanRow['addOnLoanId'];
	$loanArray['partyId']           = $loanRow['partyId'];
	$loanArray['itemId']            = $loanRow['itemId'];
	$loanArray['value']             = $loanRow['value'];
	$loanArray['loanAmount']        = $selectLoanAmountRow['totaLoanAmount'];
	$loanArray['itemName']          = $loanRow['itemName'];
	$loanArray['loanVar']           = $loanRow['loanVar'];
	$loanArray['loanVarNo']         = $loanRow['loanVarNo'];
	$loanArray['weight']            = $loanRow['weight'];
	$loanArray['itemImage']         = $loanRow['itemImage'];
	$loanArray['itemlocationName']  = $loanRow['itemlocationName'];
	$loanArray['loanDate']          = $loanRow['loanDate'];
	$loanArray['monthlyInterest']   = $loanRow['monthlyInterest'];
	$loanArray['monthlyInterestRs'] = $loanRow['monthlyInterestRs'];
}

if(isset($_POST['loanBtn']))
{
  $partyName  = $_POST['partyName'];
	$address    = $_POST['address'];
	$mobile     = $_POST['mobile'];
  $otherInfo  = $_POST['otherInfo'];
  $value      = $_POST['value'];
  $partyId    = $_POST['partyIdtxt'];
	
	
  $updateParty = "UPDATE party 
                     SET partyName   = '".$partyName."',
                         address     = '".$address."',
                         mobile      = ".$mobile.",
  	                     otherInfo   = '".$otherInfo."'
                   WHERE partyId     = ".$_POST['partyIdOld'];
  $updatePartyRes    = mysql_query($updateParty);
  
  //New Loan Add On Process
  $loanDate          = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];	
  $loanId            = $_POST['loanId'];
  $addOnLoanId       = ($_POST['addOnLoanId'] == "")? $loanId : $_POST['addOnLoanId'];
  $value             = $_POST['value'];
  $addOnAmount       = $_POST['addOnAmount'];	
  $itemId            = $_POST['itemIdtxt'];	
  $weight            = $_POST['weight'];	
  $monthlyInterest   = $_POST['monthlyInterest'];	
  $monthlyInterestRs = $_POST['monthlyInterestRs'];	
  $createFromId      = $_POST['createFromId'];
  $itemImage         = $_POST['itemImage'];
  $loanVar           = $_POST['loanVar'];
  $loanVarNo         = $_POST['loanVarNo'];
  $insLoan           = "INSERT INTO `loan` (`addOnLoanId`, `partyId`, `loanAmount` , `pendingAmount` , `value`, `itemId`, `loanDate` ,
                                            `monthlyInterest`, `monthlyInterestRs`, `createFromId`, `weight`, 
                                            `itemImage`,`loanVar`, `loanVarNo`, `lastInterestReceiveDate`)
                             VALUES (".$addOnLoanId.",".$partyId.",".$addOnAmount.", ".$addOnAmount.", ".$value.", 
                                     ".$itemId.", '".$loanDate."', ".$monthlyInterest.", ".$monthlyInterestRs.", 
                                     ".$createFromId.", ".$weight.", '".$itemImage."', '".$loanVar."','".$loanVarNo."',
                                     '0000-00-00')";
  $insLoanRes        = mysql_query($insLoan);
  header("Location:loanList.php");
}

$arrParty = array();
$i=0;
$selParty="SELECT `partyId`,`partyName` 
             FROM `party`";
$selPartyRes=mysql_query($selParty);
while($selPartyResRow=mysql_fetch_array($selPartyRes))
{
  $arrParty['partyName'][$i] = $selPartyResRow['partyName'];
  $arrParty['partyId'][$i]   = $selPartyResRow['partyId'];
  $i++;
}
///////////fetch the party Name Over

include("bottom.php");
$smarty->assign('arrParty',$arrParty);  
$smarty->assign('loanArray',$loanArray);  
$smarty->assign('createFrom',$createFrom);  
$smarty->display('loanAddOn.tpl');  
?>